package com.deng.etl.verify.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.vo.OrganizationVo;
import com.deng.etl.verify.vo.EtlValidVo;
import com.deng.etl.vo.EtlDailyVo;
import com.deng.util.DateTools;

public class EtlWorkloadValidImpl extends SQLHelper{

	
	public void validWorkload(EtlValidVo vo){
		try{
			Connection conn = getConnect();
			String sql = "select * from t_sys_etl_workload_valid " +
					"	where organization_id = "+vo.organization_id+" and stat_time = '"+vo.stat_time+"'";
			ResultSet rs = queryData(conn, sql);
			if(rs.next()){
				sql = "update t_sys_etl_workload_valid" +
						"	set valid = 1,valid_time = '"+DateTools.dtsJustDate(new Date())+
						"',valid_user_code = '"+vo.valid_user_code+"'";
			}else{
				sql = "insert into t_sys_etl_workload_valid" +
					"	(organization_id,organization_name,valid_time," +
					"	stat_time,valid,valid_user_code)" +
					"	values("+vo.organization_id+",'"+vo.organization_name+"'," +
					"	'"+DateTools.dtsJustDate(new Date())+"','"+vo.stat_time+"',1,'"+vo.valid_user_code+"')";
			}
			updateData(conn, sql);
			freeConnection(conn);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public List<EtlValidVo> searchWorkloadValidStatus(String stat_time){
		try{
			Connection conn = getConnect();
			String sql = "exec SP_GetFullNameOrganization 1";
			ResultSet rs = queryData(conn,sql);
			List<EtlValidVo> lst = new ArrayList<EtlValidVo>();
			while(rs.next()){
				EtlValidVo vo = new EtlValidVo();
				vo.name = "工作量抽取";
				vo.organization_name = rs.getString("full_name");
				vo.organization_id = rs.getInt("id");
				vo.stat_time = stat_time;
				String sql2 = "select * from t_sys_etl_workload_valid " +
						"	where stat_time = '"+stat_time+"'" +
						" 	and organization_id = "+vo.organization_id;
				ResultSet rs2 = queryData(conn, sql2);
				if(rs2.next()){
					vo.valid = rs2.getInt("valid");
					vo.valid_time = DateTools.dtsJustDate(rs2.getDate("valid_time"));
					vo.valid_user_code = rs2.getString("valid_user_code");					
				}
				lst.add(vo);
			}			
			freeConnection(conn);
			return lst;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	
}
